package com.probitcoin;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class WelcomeActivity extends Activity {

	EditText etKey;
	EditText etSecret;
	TextView error;
	Button save;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);
		
		initViews();
		
		initListeners();
		
		checkForErrors();
		
		loadPrefs();
	}
	
	
    private void loadPrefs() {
    	SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

    	String key = sp.getString("key", "Not found");
    	String secret = sp.getString("secret", "Not found");
    	
    	if (key.equals("Not found") || secret.equals("Not found")) {
    		// No saved configurations found
    	} else {
    		etKey.setText(key);
    		etSecret.setText(secret);
    	}

    }
    
    private void checkForErrors() {
    	error.setVisibility(View.GONE);
    	error.setText("");
    	
    	if(getIntent().hasExtra("ERROR")) 
    		displayError(getIntent().getStringExtra("ERROR"));

    }

    private void savePrefs(String key, String value) {
    	SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        Editor edit = sp.edit();
        edit.putString(key, value);
        edit.commit();
    }
	
    private void initViews() {
    	etKey 	 = (EditText) findViewById(R.id.etKey);
    	etSecret = (EditText) findViewById(R.id.etSecret);
    	error	 = (TextView) findViewById(R.id.error);
    	save 	 = (Button)   findViewById(R.id.save);
    }
    
    private void initListeners() {
    	save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String key = etKey.getText().toString();
				String secret = etSecret.getText().toString();
				
				if(correctKeyAndSecret(key, secret)) {
				
					savePrefs("key", key);
					savePrefs("secret", secret);
		    		
					Intent i = new Intent().setClass(getApplicationContext(), MainActivity.class);
		    		startActivity(i);
		    		finish();
		    		
				} else {
					displayError("INCORRECT_FORMAT");
				}
			}
		});
    }
    
    private boolean correctKeyAndSecret(String key, String secret) {
    	if (key.length() == 44 || secret.length() == 64) {
    		return true;
    	} else return false;
    }
    
    private void displayError(String type) {
    	if(type.equals("INIT_FAILED")) {
    			error.setText(R.string.verify);
    			error.setVisibility(View.VISIBLE);
		}
    	if(type.equals("INIT_FAILED")) {
    			error.setText(R.string.init);
    			error.setVisibility(View.VISIBLE);
		}
    	if(type.equals("INCORRECT_FORMAT")) {
    		error.setText(R.string.format);
			error.setVisibility(View.VISIBLE);
    	}
    }
    
//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.welcome, menu);
//		return true;
//	}

}
